package gui;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;

import datatypes.Stadt;
import datatypes.Ware;

public class AusgabeElementPanel extends JPanel {

	private static final long 	serialVersionUID = 8465112736791007914L;
	GridBagConstraints 			gc;
	
	JLabel 						stadtLabel;
	String						stadt 				=	"";
	ArrayList<Ware>				warenListe;
	int 						warenAnzahl 		=	-1;
	int							row 				=	-1;
	int							column 				=	-1;
	int 						width				=	350;
	int							lineheight			=	20;
	int							maxheight 			=	0;
	
	Insets						defaultlayoutInset	=	new Insets(2, 5, 2, 5);
	
	
	public AusgabeElementPanel(Stadt stadt, ArrayList<Ware> warenInputListe) {
		super();
		this.stadt = stadt.toString();
		
		initComponents(warenInputListe);
		initGridBag();
		
		TitledBorder b = BorderFactory.createTitledBorder(this.stadt);
		b.setTitleJustification(TitledBorder.CENTER);
		setBorder(b);
		
		setLayout(new GridBagLayout());
		
		addAllGoods();
		setVisible(true);
	}
	
	private void initComponents(ArrayList<Ware> warenliste) {
		this.gc = new GridBagConstraints();
		
		this.warenListe 	= new ArrayList<Ware>();
		this.stadtLabel 	= new JLabel(stadt);
		this.warenListe 	= warenliste;
		this.warenAnzahl 	= this.warenListe.size();
		
		this.row 			= 0;
		this.column 		= 0;
	}
	
	private void initGridBag(){
		gc.anchor = GridBagConstraints.LINE_START;
		gc.fill = GridBagConstraints.HORIZONTAL;
		gc.insets = defaultlayoutInset;
		gc.weightx = 1;
		gc.weighty = 1;
	}
	
	public void addGoodToPanel(Ware w){
		int numberOfCities = w.getVerkaufsorte().size();
		int currentRow = row;
//		System.out.println("1: currentRow: " +currentRow);
		//add good
		gc.gridx = 0;
		gc.gridy = currentRow;
//		System.out.println("2: Good= " + w.getName() + " gridx = "+gc.gridx + " gridy = "+gc.gridy);
		add(new JLabel(w.getName()), gc);
		
		gc.gridx = 1;
		gc.gridy = currentRow;
		gc.fill = GridBagConstraints.NONE;
		add(new JLabel(String.valueOf(w.getStufe())), gc);
		
		gc.fill = GridBagConstraints.HORIZONTAL;
		
		//add citesforthegood
		for (int i = 0; i < numberOfCities; i++) {
			//add single good
			gc.gridx = 2;
			gc.gridy = currentRow+i;
//				System.out.println("3: city= " + w.getVerkaufsorte().get(i) + " gridx = "+gc.gridx + " gridy = "+gc.gridy);
			JLabel tmpLabel = new JLabel(w.getVerkaufsorte().get(i));
			add(tmpLabel, gc);
		}
		//set row to tmpRow+Anzahl Verkaufsorte
		this.row= row +numberOfCities;
	}
	
	public void addAllGoods(){
		int numberOfCities = 0;
		for (Ware ware : warenListe) {
			numberOfCities += ware.getVerkaufsorte().size();
			addGoodToPanel(ware);
		}
		this.maxheight = lineheight+ (lineheight+defaultlayoutInset.top+defaultlayoutInset.bottom)*numberOfCities;
		setMinimumSize(new Dimension(width, maxheight));
		setPreferredSize(new Dimension(width, maxheight));
		setMaximumSize(new Dimension(width, maxheight));
	}

	public int getMaxheight() {
		return maxheight;
	}

	public void setMaxheight(int maxheight) {
		this.maxheight = maxheight;
	}
}
